﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using log4net;
using System.Data;
using QualityRpt.Service;
using System.Text;

namespace QualityRPT.AirwirePlaza
{
    public partial class edit : System.Web.UI.Page
    {
        public string supplier_name_json = string.Empty;
        public string spec_name_json = string.Empty;
        public string road_name_json = string.Empty;
        ILog logger = LogManager.GetLogger("CARDContract.Edit");
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                #region combox列表设置
                DataTable spTable = DeviceSupplierService.GetListSupplierName(string.Empty, string.Empty, DeviceService.AIRWIRE_DEV);
                if (spTable.Rows.Count > 0)
                {
                    StringBuilder sbText = new StringBuilder();
                    for (int i = 0; i < spTable.Rows.Count; i++)
                    {
                        if (i > 0)
                        {
                            sbText.Append(",");
                        }
                        sbText.Append("'" + spTable.Rows[i]["SUPPLIER_NAME"] + "'");
                    }
                    supplier_name_json = sbText.ToString();
                }
                DataTable scTable = DeviceSpecService.GetBySpIn(string.Empty, DeviceService.AIRWIRE_DEV);
                if (scTable.Rows.Count > 0)
                {
                    StringBuilder sbScnameText = new StringBuilder();
                    for (int i = 0; i < scTable.Rows.Count; i++)
                    {
                        if (i > 0)
                        {
                            sbScnameText.Append(","); 
                        }
                        sbScnameText.Append("'" + scTable.Rows[i]["SPEC_NAME"] + "'");
                    }
                    spec_name_json = sbScnameText.ToString();
                }
                DataTable rdTable = NetWorkPlazaService.GetRoadList();//获得所有路段
                if (rdTable.Rows.Count > 0)
                {
                    StringBuilder sbRdnameText = new StringBuilder();
                    for (int i = 0; i < rdTable.Rows.Count; i++)
                    {
                        if (i > 0)
                        {
                            sbRdnameText.Append(",");
                        }
                        sbRdnameText.Append("'" + rdTable.Rows[i]["ORGAN_NAME"] + "'");
                    }
                    road_name_json = sbRdnameText.ToString();
                }
                #endregion
                #region 编辑加载数据
                string strid = Request.Params["id"];
                if (strid != null && strid != string.Empty)
                {
                    h_rowid.Value = strid;
                    DataRow eRow = AirwirePlazaService.Get(Convert.ToInt64(strid));
                    if (eRow != null)
                    {
                        this.txt_plaza_id.Value = eRow["PLAZA_ID"].ToString();
                        this.txt_plaza_name.Value = eRow["PLAZA_NAME"].ToString();
                        this.txt_road_id.Value = eRow["ROAD_ID"].ToString();
                        this.txt_road_name.Value = eRow["ROAD_NAME"].ToString();
                        this.txt_lane_no.Value = eRow["LANE_NO"].ToString();
                        this.txt_lane_mode.Value = eRow["LANE_MODE"].ToString();
                        this.txt_supplier_name.Value = eRow["AIRWIRE_SUPPLIER_NAME"].ToString();
                        DataRow eSpRow = DeviceSupplierService.GetByName(eRow["AIRWIRE_SUPPLIER_NAME"].ToString());
                        if (eSpRow != null)
                        {
                            this.txt_supplier_shortname.Value = eSpRow["SUPPLIER_SHORTNAME"].ToString();
                        }
                        this.txt_spec_name.Value = eRow["AIRWIRE_SPEC_NAME"].ToString();
                        DataRow eScRow = DeviceSpecService.GetByName(eRow["AIRWIRE_SPEC_NAME"].ToString());
                        if (eScRow != null)
                        {
                            this.txt_spec_shortname.Value = eScRow["SPEC_SHORTNAME"].ToString();
                        }
                        this.txt_sortversion.Value = eRow["AIRWIRE_SOFTVERSION"].ToString();
                        this.txt_hardversion.Value = eRow["AIRWIRE_HARDVERSION"].ToString();
                        this.txt_start_date.Value = eRow["START_TIME"].ToString();
                        this.txt_end_date.Value = eRow["END_TIME"].ToString();
                        this.airwire_type.Value = eRow["AIRWIRE_TYPE"].ToString();
                        this.txt_remark.Value = eRow["REMARK"].ToString();
                    }
                }
                #endregion
            }
        }
        protected void ButtonSave_Click(object sender, EventArgs e)
        {
            SaveData();
            Response.Redirect("index.aspx?pid=" + cltop.pid + "&cid=" + clleft.cid + "&nocache=" + DateTime.Now.Ticks);
        }
        protected void ButtonContinue_Click(object sender, EventArgs e)
        {
            SaveData();
            Response.Redirect("edit.aspx?pid=" + cltop.pid + "&cid=" + clleft.cid + "&nocache=" + DateTime.Now.Ticks);
        }
        private void SaveData()
        {
            string strhid = Request.Params["h_rowid"];
            DataRow curRow = AirwirePlazaService.GetSchema().NewRow();
            //global param
            string strroadname = Request.Params["txt_road_name"];
            string strroadid = Request.Params["txt_road_id"];
            DataRow rdRow = NetWorkPlazaService.GetRoadByName(strroadname);
            if (rdRow == null)
            {
                rdRow = NetWorkPlazaService.GetSchema().NewRow();
                rdRow["ORGAN_ID"] = strroadid; rdRow["ORGAN_CHARACTER"] = 2;
                rdRow["ORGAN_NO"] = strroadid; rdRow["ADMINSUPER_ID"] = 0;
                rdRow["ORGAN_NAME"] = strroadname; rdRow["STATE"] = 0;
                rdRow["ORGAN_ALIAS"] = strroadname; 
                rdRow = NetWorkPlazaService.Save(rdRow);
            }
            curRow["ROAD_ID"] = strroadid; curRow["ROAD_NAME"] = strroadname; 
            string strplazaname = Request.Params["txt_plaza_name"];
            string strplazaid = Request.Params["txt_plaza_id"];
            DataRow plazaRow = NetWorkPlazaService.GetPlazaByName(strplazaname, Convert.ToInt32(rdRow["ORGAN_ID"]));
            if (plazaRow == null)
            {
                plazaRow = NetWorkPlazaService.GetSchema().NewRow();
                plazaRow["ORGAN_ID"] = strplazaid; plazaRow["ORGAN_CHARACTER"] = 3;
                plazaRow["ORGAN_NO"] = strplazaid; plazaRow["ADMINSUPER_ID"] = strroadid;
                plazaRow["ORGAN_NAME"] = strplazaname; plazaRow["STATE"] = 0;
                plazaRow["ORGAN_ALIAS"] = strplazaname;
                plazaRow = NetWorkPlazaService.Save(plazaRow);
            }
            curRow["PLAZA_ID"] = strplazaid; curRow["PLAZA_NAME"] = strplazaname;
            string strsuppliername = Request.Params["txt_supplier_name"];
            string strsuppliershortname = Request.Params["txt_supplier_shortname"];
            DataRow esupRow = DeviceSupplierService.GetByName(strsuppliername);
            if (esupRow == null)//如果没有厂商信息先存储
            {
                esupRow = DeviceSupplierService.GetSchema().NewRow();
                esupRow["SUPPLIER_NAME"] = strsuppliername;
                esupRow["SUPPLIER_SHORTNAME"] = strsuppliershortname;
                esupRow = DeviceSupplierService.Save(esupRow);
            }
            curRow["AIRWIRE_SUPPLIER_ID"] = esupRow["ID"]; curRow["AIRWIRE_SUPPLIER_NAME"] = strsuppliername;
            string strspecname = Request.Params["txt_spec_name"];
            string strspecshortname = Request.Params["txt_spec_shortname"];
            DataRow especRow = DeviceSpecService.GetByName(strspecname);
            if (especRow == null)
            {
                especRow = DeviceSpecService.GetSchema().NewRow();
                especRow["SUPPLIER_ID"] = esupRow["ID"];
                especRow["DEVICE_ID"] = DeviceService.ICARD_DEV;
                especRow["SPEC_NAME"] = strspecname;
                especRow["SPEC_SHORTNAME"] = strspecshortname;
                especRow = DeviceSpecService.Save(especRow);
            }
            curRow["AIRWIRE_SPEC_ID"] = especRow["ID"]; curRow["AIRWIRE_SPEC_NAME"] = strspecname;
            string strlane_no = Request.Params["txt_lane_no"]; string strlane_mode = Request.Params["txt_lane_mode"];
            curRow["LANE_NO"] = strlane_no; curRow["LANE_MODE"] = strlane_mode;
            string strsoft_version = Request.Params["txt_sortversion"]; string strhard_version = Request.Params["txt_hardversion"];
            curRow["AIRWIRE_SOFTVERSION"] = strsoft_version; curRow["AIRWIRE_HARDVERSION"] = strhard_version;
            string strstart_date = Request.Params["txt_start_date"]; string strend_date = Request.Params["txt_end_date"];
            curRow["START_TIME"] = strstart_date; curRow["END_TIME"] = strend_date;
            string strairwire_type = Request.Params["airwire_type"]; string strremark = Request.Params["txt_remark"];
            curRow["AIRWIRE_TYPE"] = strairwire_type; curRow["REMARK"] = strremark; curRow["CREATED_TIME"] = DateTime.Now;
            if (strhid != null && strhid.Trim() != string.Empty)
            {
                curRow["ID"] = strhid;
                AirwirePlazaService.Update(curRow);
            }
            else
            {
                AirwirePlazaService.Save(curRow);
            }
        }
    }
}